<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*This library handles the property details adding forms. It gets the submitted form
 * id and validates it before inserting to the database*/
class DataEntryForms
{
	private $CI;
###################################################################################################################################
	//gets the CI instance	
	function __construct(){
		$this->CI =& get_instance();
		if(!isset($this->CI->session)){
			$this->CI->load->library('session');
		}
	}

###################################################################################################################################
	//validate and save the 'form01'	
	function form1()
	{
		$this->CI->load->library('form_validation');
		/*$post_data = ($this->CI->input->post());
		foreach ($post_data as $field => $value){
			$this->CI->form_validation->set_rules('projectname','project name','required|xss_clean');
		}*/
	
		//setting cutom validation errors
		$this->CI->form_validation->set_message('required', '%s cannot be empty');
		$this->CI->form_validation->set_message('max_length', '%s should be less than %s characters');
		$this->CI->form_validation->set_message('integer', '%s should be a valid number');
		$this->CI->form_validation->set_message('numeric', '%s should be a valid distance');
		
		//setting validation rules
		$this->CI->form_validation->set_rules('form01_company_selector','Company name','xss_clean|max_length[45]');
		$this->CI->form_validation->set_rules('project_type','Company','xss_clean|required|max_length[45]');
		$this->CI->form_validation->set_rules('project_name','Project name','xss_clean|max_length[45]');
		$this->CI->form_validation->set_rules('address_street','Street','required|xss_clean|max_length[45]');
		$this->CI->form_validation->set_rules('address_suburb','Suburb','xss_clean|max_length[45]');
		$this->CI->form_validation->set_rules('address_town','Town','required|xss_clean|max_length[45]');
		$this->CI->form_validation->set_rules('address_district','District','required|xss_clean|max_length[45]');
		$this->CI->form_validation->set_rules('datelaunched','Date launched','xss_clean|max_length[10]');
		$this->CI->form_validation->set_rules('introducer','Introducer','required|xss_clean');	
		$this->CI->form_validation->set_rules('competitor','Competitor avilability','required|xss_clean');
		$this->CI->form_validation->set_rules('project_area_situation','Area situation','required|xss_clean');
		$this->CI->form_validation->set_rules('howmanyprolandsinthearea','Prospective lands amount','xss_clean|integer');
		$this->CI->form_validation->set_rules('closestprospectivelands','Closest prospective land distance','xss_clean|numeric');
	
		if($this->CI->form_validation->run() != false){
			$this->CI->load->model('insertdata_model');
			
			if($this->CI->input->post('howmanyprolandsinthearea') == ''){
				$prospective_lands_amount = NULL;
			}
			
			else{
				$prospective_lands_amount = $this->CI->input->post('howmanyprolandsinthearea');
			}
			
			if($this->CI->input->post('howmanyprolandsinthearea') == ''){
				$prospective_land_distance = NULL;
			}
				
			else{
				$prospective_land_distance = $this->CI->input->post('howmanyprolandsinthearea');
			}
			
			$params = array($this->CI->input->post('form01_company_selector'),
							$this->CI->session->userdata('username'),
							$this->CI->input->post('project_type'),
							$this->CI->input->post('project_name'),
							$this->CI->input->post('address_street'),
							$this->CI->input->post('address_suburb'),
							$this->CI->input->post('address_town'),
							$this->CI->input->post('address_district'),
							$this->CI->input->post('datelaunched'),
							$this->CI->input->post('introducer'),
							$this->CI->input->post('competitor'),
							$this->CI->input->post('project_area_situation'),
							$prospective_lands_amount,
							$prospective_land_distance);
			
			$query = 'INSERT INTO project_maindetails 
						(company_id,added_by,project_type,project_name,project_street,project_suburb, project_town,
							project_district, project_launched_date, project_introducer, other_project_owners, project_area_situation,
								prospective_lands_avail, closest_prospective_distance)
								VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
			
			$callback = true;
			
			//send the post data to the database while requesting a property id for other entries
			$propertyid = $this->CI->insertdata_model->insertdata(false,$query,$params);
			
			if($propertyid != false){
				$this->CI->session->set_userdata('propertyid',$propertyid);
				echo '{"validation_result": "passed"}';
			}
			
			else{
				echo '{"validation_result": "Could not preform the action. Please contact the administrator"}';
			}
			
				
		}
		else
		{
			//if validation error happens, we need to echo it as a json object
			//echo json_encode('{"validation_result":"failed","errors":"'.validation_errors('','').'"}');
			$output = '{"validation_result":"failed", 
						"project_type":"'.form_error('project_type').'",
						"project_name":"'.form_error('project_name').'",
						"address_street":"'.form_error('address_street').'",
						"address_suburb":"'.form_error('address_suburb').'",
						"address_suburb":"'.form_error('address_town').'",
						"address_district":"'.form_error('address_district').'",
						"datelaunched":"'.form_error('datelaunched').'",
						"introducer":"'.form_error('introducer').'",
						"competitor":"'.form_error('competitor').'",
						"project_area_situation":"'.form_error('project_area_situation').'",
						"howmanyprolandsinthearea":"'.form_error('howmanyprolandsinthearea').'",
						"closestprospectivelands":"'.form_error('closestprospectivelands').'" }';
			echo $output;
	
		}
	}
	
##################################################################################################################################	
	function form2()
	{
	
		// sort bus root numbers
	
		//$_POST['bustour_number']  = array_values($_POST['bustour_number']);
	
		/*
		$_POST['nameoftown'][]
		$_POST['roaddiscription'][]
		$_POST['towndistence'][]
		$_POST['rootdiscription'][]
		$_POST['rootdistance'][]
		$_POST['bustour_number'][]
		$_POST['station_name'][]
		$_POST['station_distance'][]
		$_POST['nameof_bank'][]
		$_POST['distence_bank'][]
		$_POST['supname'][]
		$_POST['supdistence'][]
		$_POST['hosname'][]
		$_POST['hosdistence'][]
		$_POST['schname'][]
		$_POST['schdistence'][]
		$_POST['txt_temple'][]
		$_POST['txt_church'][]
		$_POST['txt_kovil'][]
		$_POST['txt_mosque'][]
		$_POST['txt_police'][]
		$_POST['txt_mc'][]
		$_POST['txt_ds'][]
		$_POST['txt_postoffice'][]
		$_POST['txt_courts'][]
		$_POST['txt_cemetery'][]
		$_POST['txt_rockquarry'][]
		$_POST['txt_pigsty'][]
		$_POST['txt_chicken'][]
		$_POST['txt_cocunetpit'][]
		$_POST['txt_nostyfactories'][]
		$_POST['txt_shanties'][]
		
		*/
		
		$this->CI->load->library('form_validation');
		
		//setting custom validation error messages
		$this->CI->form_validation->set_message('required','%s cannot be blanked');
		
		
		
		/*validating the dynamic input fields*/
		//setting validation rules to the 'Distance to town' section
		$town_names = $this->CI->input->post('nameoftown');
		//echo print_r($town_names);
		//die();
		for ($i = 0; $i < count($town_names); $i++) {
				$this->CI->form_validation->set_rules('nameoftown['.$i.']', 'Road description/s', 'xss_clean|required');
		}
		
		//$this->CI->form_validation->set_rules('nameoftown[]', 'Town name/s', 'xss_clean|required|max_length[45]|minimum_length[3]');
		//echo 'nameoftown'.$input_id.' - '.$input_value.'<br />';
		
		//echo $this->CI->input->post('nameoftown');
		
		
		//setting validation rules to the 'Road description' section
		$road_descriptions = array_values($this->CI->input->post('road_description'));
		//echo print_r($road_descriptions);
		//die();
		/*for ($i = 0; $i < count($road_descriptions); $i++) {
			foreach($road_descriptions[$i] as $input_id => $input_value){
				$this->CI->form_validation->set_rules($input_id[], 'Road description/s', 'xss_clean|required');
			}
		}*/
		
		
		
		if($this->CI->form_validation->run() != false){
			
			
		}
		
		else{
			
			/*$json_string = '{"validations":[{"validation_result":"failed"}], "town_names_errors":[{';
			
			//insert the validation errors of 'town names' in to json
				if(form_error('nameoftown[]')){
					foreach($town_names as $input_id => $input_value){
						$json_string .= '"'.'nameoftown'.$input_id.'":"'.form_error('nameoftown[]').'",';
				}
			}
			
			$json_string .= '}],"road_description_errors":[{';
			
			//insert the validation errors of 'road descriptions' in to json
			for ($i = 0; $i < count($road_descriptions); $i++) {
				foreach($road_descriptions[$i] as $input_id => $input_value){
					if(form_error('road_description'.$input_id)){
						$json_string .= '"town'.$i.'_road_description'.$input_id.'":"'.form_error('road_description'.$input_id).'",';
					}
				}
			}
			
			
			$json_string .= '}]}';
			
			echo $json_string;
			
			//echo form_error('nameoftown[]');*/
			echo validation_errors();
		}
		
		
		/*$this->CI->form_validation->set_rules('nameoftown');
		//$this->form_validation->set_rules('towndistence[]', 'Distence', 'xss_clean|callback_nullornumaric_check');
		//$this->form_validation->set_rules('rootdistance[]', 'Distence', 'xss_clean|callback_nullornumaric_check');
	
	
		for($i=0; $i<count($this->input->post('bustour_number[]')); $i++)
		{
		$this->form_validation->set_rules('bustour_number['.$i.'][]', 'Distence', 'xss_clean|callback_nullornumaric_check');
					
		}
	
	
		$this->form_validation->set_rules('station_distance[]', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('distence_bank[]', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('hosdistence[]', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('schdistence[]', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_temple', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_church', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_kovil', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_mosque', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_police', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_mc', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_ds', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_postoffice', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_courts', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_cemetery', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_rockquarry', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_pigsty', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_chicken', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_cocunetpit', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_nostyfactories', 'Distence', 'xss_clean|callback_nullornumaric_check');
		$this->form_validation->set_rules('txt_shanties', 'Distence', 'xss_clean|callback_nullornumaric_check');


		if($this->form_validation->run() != false){

			echo "true";

		}
		else
		{
			echo validation_errors();
		}
	
	}
	
	
	//check null or numaric
	
	
	public function nullornumaric_check($str)
	{
		if(($str==null)||preg_match("/^[0-9\.]+$/i",$str))
		{
			return true;
		}
		else
		{
			$this->form_validation->set_message('nullornumaric_check', 'Distence must content numbers');
			return false;
		}
		
		*/
	}
	
	
	
	
	
	
	
	function form3()
	{
	
		echo "true";
		/*
		echo "<pre>";
		print_r($_POST);
		echo "</pre>";
		
		$this->load->library('form_validation');
		
		
		if($this->form_validation->run() != false){
		
		echo "true";
		
		}
		else
		{
		echo validation_errors();
		}
		*/
	}
	function form4()
	{
	
		echo "true";
	
		/*
		echo "<pre>";
		print_r($_POST);
			echo "</pre>";
	
	
	
	
		$this->load->library('form_validation');
	
	
		if($this->form_validation->run() != false){
	
		echo "true";
	
		}
		else
		{
		echo validation_errors();
		}
		*/
	}
	
###################################################################################################################################
	//validates and save the form 05	
	function form5()
	{
		$this->CI->load->library('form_validation');

		$this->CI->form_validation->set_rules('pipewater','Pipe born water','xss_clean');
		$this->CI->form_validation->set_rules('waterpipedistance','Distance to main water line','xss_clean');
		$this->CI->form_validation->set_rules('depthwater','Groud water depth','xss_clean|numeric');
		$this->CI->form_validation->set_rules('depthwatercondition','Groud water condition','xss_clean');
		$this->CI->form_validation->set_rules('water_remarks','Water remarks','xss_clean');
		$this->CI->form_validation->set_rules('epace','Electricity phase','xss_clean|integer');
		//get the property id from session
		$property_id = $this->CI->session->userdata('propertyid');
		
		/*
		$this->load->library('form_validation');
	
	
		if($this->form_validation->run() != false){
	
		echo "true";
	
		}
		else
		{
		echo validation_errors();
		}
		*/
	}
	function form6()
	{

		echo "true";
	
		/*
		$this->load->library('form_validation');
	
	
		if($this->form_validation->run() != false){
	
		echo "true";
	
		}
		else
		{
		echo validation_errors();
		}
		*/
	}
	function form7()
	{


		echo "<pre>";
		print_r($_POST);
		echo "</pre>";
		/*echo "true";
	
	
		$this->load->library('form_validation');
	
	
		if($this->form_validation->run() != false){
		
		 echo "true";
		
		 }
		 else
		 {
		 echo validation_errors();
		 }
		 */
	 }
	 function form8()
	 {
	
		 echo "true";
		
		
		 /*
		 $this->load->library('form_validation');
		
		
		 if($this->form_validation->run() != false){
		
			echo "true";
		
		}
		else
			{
			echo validation_errors();
			}
		*/
	}
}